package com.lap.dev.starter.core;

import com.lap.dev.starter.shared.NetworkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;

@Slf4j
public class LocalIpEnvironmentPostProcessor implements EnvironmentPostProcessor {

  @Override
  public void postProcessEnvironment(
      ConfigurableEnvironment environment, SpringApplication application) {
    if (environment.matchesProfiles("dev", "local")) {
      String localIp = NetworkUtil.getPrimaryIPv4Address();
      if (localIp != null) {
        // 获取本地IP并设置到系统属性
        log.info("对环境变量进行设置:{}", localIp);
        System.setProperty("localIp", localIp);
      }
    } else {
      log.debug("非开发环境，跳过本地IP设置");
    }
  }
}
